package com.arithmeticEveryDay.blueBridge;

import java.util.Scanner;

/*

5
12349
54321

9+2-->12351,5-3-->12321,2+2-->14321,1+4-->54321
11

99994
00000

4+6-->00000
4-4-->99990，9+1-->00000
从这个例子可以看出，局部最优好像能推出全局最优，4到0，+6和-4，因为操作数少所以选择-4，一共只需要5次操作即可

99995
00000

5+5-->00000
5-5-->99990
然而你再看看这个例子，局部最优退不出全局最优了，+5和-5都是5，都是最优解，但+5才能推出全局最优解


99905
00000

5+5-->99910, 1-2+1-->00000
5-5-->99900, 9+1-->00000


其实如果局部有多个最优解的话那么就分支继续找最优解呗，就能推出全局最优解




 */

public class Main3545 {
    
    int[] originNum;
    int[] targetNum;
    
    
    private Main3545(int[] originNum, int[] targetNum) {
        super();
        this.originNum = originNum;
        this.targetNum = targetNum;
    }
    
    private int getminOperateNum(int i) {
        //System.out.println(i + Arrays.toString(originNum));
        if (i < 0) {
            return 0;
        }
        int deficit = targetNum[i] - originNum[i];
        if (deficit == 0) {
            return getminOperateNum(i-1);
        } else if (deficit == 5 || deficit == -5) {
            int[] record = new int[originNum.length];
            for (int j = 0; j < originNum.length; j++) {
                record[j] = originNum[j];
            }
            // 若+5
            originNum[i] += 5;
            for (int j = i; j >= 0 && originNum[j] + 5 >= 10; j--) {
                originNum[j] -= 10;
                if (j > 0) {
                    originNum[j-1]++;
                }
            }
            int add5Result = getminOperateNum(i-1);
            // 还原originNum
            for (int j = 0; j < originNum.length; j++) {
                originNum[j] = record[j];
            }
            // 若-5
            originNum[i] -= 5;
            for (int j = i; j >= 0 && originNum[j] < 0; j--) {
                originNum[j] += 10;
                if (j > 0) {
                    originNum[j-1]--;
                }
            }
            int minus5Result = getminOperateNum(i-1);
            return 5 + (add5Result < minus5Result ? add5Result : minus5Result);
        } else if (deficit > 0) {
            int add = deficit;
            int minus = 10 - deficit;
            if (add > minus) {
                // 向i-1借1位
                originNum[i] -= deficit;
                for (int j = i; j >= 0 && originNum[j] < 0; j--) {
                    originNum[j] += 10;
                    if (j > 0) {
                        originNum[j-1]--;
                    }
                }
                return minus + getminOperateNum(i-1);
            } else {
                originNum[i] += add;
                return add + getminOperateNum(i-1);
            }
            
        } else if (deficit < 0) {
            int add = 10 + deficit;
            int minus = -deficit;
            if (add < minus) {
                // 向i-1进1位
                originNum[i] += add;
                for (int j = i; j >= 0 && originNum[j] >= 10; j--) {
                    originNum[j] -= 10;
                    if (j > 0) {
                        originNum[j-1]++;
                    }
                }
                return add + getminOperateNum(i-1);
            } else {
                originNum[i] -= minus;
                return minus + getminOperateNum(i-1);
            }
            
        }
        return 0;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        char[] chNum1 = scan.next().toCharArray();
        char[] chNum2 = scan.next().toCharArray();
        int[] originNum = new int[n];
        int[] targetNum = new int[n];
        for (int i = 0; i < n; i++) {
            originNum[i] = chNum1[i] - '0';
            targetNum[i] = chNum2[i] - '0';
        }
        Main3545 main = new Main3545(originNum, targetNum);
        System.out.println(main.getminOperateNum(n-1));
    }

    



    

}
